#include "stdafx.h"
#include "BoundingBox.h"
#include "BoundingSphere.h"
#include "Math.h"


BoundingBox::BoundingBox(void)
{
	memset(this, 0, sizeof(BoundingBox));
}

BoundingBox::BoundingBox(Vector3& position, float size)
{
	this->position = position;
	front = back = left = right = top = bottom = size;
}

BoundingBox::BoundingBox(Vector3& position, float front, float back, float left, float right, float top, float bottom)
{
	this->position = position;
	this->front = front;
	this->back = back;
	this->left = left;
	this->right = right;
	this->top = top;
	this->bottom = bottom;
}
/*virtual*/ bool BoundingBox::Intersects(IBoundingShape* other) /*override*/
{
	switch (other->getType())
	{
	case BOUNDING_SHAPE_CUBE:
	{
		BoundingBox* box = (BoundingBox*)other;
		Vector3 point_rel = box->position - position;
		float left = Math::max(getWorldLeft(), box->getWorldLeft());
		float top = Math::max(getWorldTop(), box->getWorldTop());
		float front = Math::max(getWorldFront(), box->getWorldFront());
		float right = Math::min(getWorldRight(), box->getWorldRight());
		float bottom = Math::min(getWorldBottom(), box->getWorldBottom());
		float back = Math::min(getWorldBack(), box->getWorldBack());

		float width = right - left;
		float height = bottom - top;
		float depth = back - front;

		return width > 0.f && width <= computeWidth() && height > 0.f && height <= computeHeight() && depth > 0.f && depth <= computeDepth();
	}
	case BOUNDING_SHAPE_SPHERE:
	{
		BoundingSphere* sphere = (BoundingSphere*)other;

		float dist_x = Math::distance(sphere->position.x, position.x);
		float dist_y = Math::distance(sphere->position.y, position.y);
		float dist_z = Math::distance(sphere->position.z, position.z);

		return dist_x < computeWidth() + sphere->radius && dist_y < computeHeight() + sphere->radius && dist_z < computeDepth() + sphere->radius;
	}
	}
	return false;
}

/*virtual*/ BoundingShapeType BoundingBox::getType(void) const /*override*/
{
	return BOUNDING_SHAPE_CUBE;
}